
<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="110px"
    >
      <el-form-item label="订单号" prop="orderNumber">
        <el-input
          v-model="queryParams.orderNumber"
          placeholder="请输入订单号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="产品编号" prop="productNumber">
        <el-input
          v-model="queryParams.productNumber"
          placeholder="请输入产品编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="客户名称" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="setSearchParams(queryParams)" type="primary"> 搜索</el-button>
        <el-button @click="resetQuery"> 重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <div class="btnWrap">
      <div>
        <el-button type="primary" @click="handleExport" v-hasPermi="['order:contractAssess:export']" :loading="exportLoading">
          导出数据
        </el-button>
        <el-button type="primary" @click="openForm()" v-hasPermi="['order:contractAssess:update']">
          编辑
        </el-button>
      </div>
      <div>
        <el-button type="primary" @click="openTableTitleSet"> 表头设置 </el-button>
      </div>
    </div>
    <Table
      :columns="tableTitleList"
      :selection="tableObject.tableList.length ? true : false"
      @register="register"
      @update:currentPage="getTableData"
      :data="tableObject.tableList"
      :loading="tableObject.loading"
      :pagination="{
        total: tableObject.total
      }"
      v-model:pageSize="tableObject.pageSize"
      v-model:currentPage="tableObject.pageNo"
      :tableHeight="tableHeight"
    />
  </ContentWrap>
  <!-- 导出 -->
  <ExportFile ref="exportRef" @download="downloadFun" />
  <!-- 表头设置 -->
  <table-width-set ref="tableTitleSet" @refresh="getLabelAll" />
</template>

<script setup lang="ts" name="cafEdit">
  import download from '@/utils/download'
  import * as ContractAssessApi from '@/api/orderManagement/contractAssess'
  import type { CrudSchema } from '@/hooks/web/useCrudSchemas'
  const { push } = useRouter()
  let tableTitleList = reactive<CrudSchema[]>([]) // 表头的数据
  // const message = useMessage() // 消息弹窗
  // const { t } = useI18n() // 国际化
  const loading = ref(true) // 列表的加载中
  const queryParams = reactive({
    pageNo: 1,
    pageSize: 10,
    orderNumber: null,
    productNumber: null,
    productName: null,
    customerName:undefined
  })
  const queryFormRef = ref() // 搜索的表单
  const exportLoading = ref(false) // 导出的加载中
  // 获得表格的各种操作
  const { tableObject, tableMethods } = useTable({
    getListApi: ContractAssessApi.getContractAssessPage, // 分页接口
    defaultParams: queryParams
  })
  const parentRef = ref()
  // 获取table组件
  const register = (parent: any) => {
    parentRef.value = parent
  }
  const { getList, setSearchParams } = tableMethods
  const getLabelAll = async () => {
    loading.value = true
    try {
      const data = await ContractAssessApi.getContractAssessPage(queryParams)

      const { allSchemas } = useCrudSchemas(data.labels)
      tableTitleList = allSchemas.tableColumns
      getList()
    } finally {
      loading.value = false
    }
  }
  /** 查询列表 */
  const getTableData = async (val) => {
    tableObject.currentPage = val
    getList()
  }

  /** 搜索按钮操作 */
  const handleQuery = () => {
    queryParams.pageNo = 1
    getList()
  }

  /** 重置按钮操作 */
  const resetQuery = () => {
    queryFormRef.value.resetFields()
    handleQuery()
  }

  const openForm = () => {
    push('/orderManagement/orderManagement/singleContractBatchManagement/contractAssessInfoEdit')
  }
  /** 导出按钮操作 */
  const exportRef = ref()
  const handleExport = () => {
    exportRef.value.open()
  }
  //导出数据
  const downloadFun = async () => {
    const val = exportRef.value.params
    let params = {
      fileName: val.name,
      ids: ''
    }
    console.log(val.range)
    if (val.range === 'all') {
      params.ids = ''
    } else {
      parentRef.value.selections.map((item) => {
        params.ids += (item as any).id + ','
      })
    }
    const data = await ContractAssessApi.exportContractAssess(params)
    download.excel(data, val.name)
    parentRef.value.clearTableSelect()
  }
  /**打开表格表头设置**/
  const tableTitleSet = ref()
  const openTableTitleSet = () => {
    const arr = tableTitleList
    let code = 'ContractAssess'
    tableTitleSet.value.open(arr, code)
  }

  /** 初始化 **/
  const tableHeight = ref(0)
  //132：表格内除表格以外的高度，127 顶部固定高度 + copyright高度
  onMounted(async () => {
    const queryParamsHeight = queryFormRef.value.$el.clientHeight + 2
    tableHeight.value = document.body.clientHeight - 132 - queryParamsHeight - 127
    await getLabelAll()
    getList()
  })
</script>
<style lang="scss" scoped>
  .btnWrap {
    display: flex;
    justify-content: space-between;
    margin-bottom: 10px;
  }
</style>
